import request from '@/utils/request';
import { getModelData } from '@/utils/utils';
import { getDvaApp, history, getLocale } from 'umi';
export async function getSystemParamsByValue(props: string) {
  try {
    let value = null;
    const res = await request('/parameter/getByName', {
      data: { value: props },
      method: 'POST',
    });
    if (res) {
      const systemParameters = getModelData('config.systemParameters');
      const Locale = getLocale() === 'zh-CN' ? 'zh_CN' : 'en';
      value = res.value;
      if (props === 'FW_DEMO_PARAM_STRING_I18N' || props === 'USER_PASSWORD_VALIDATE_PROMPT') {
        value = JSON.parse(res.value)[Locale] || '';
      }
      const { dispatch } = getDvaApp()._store;
      dispatch({
        type: 'config/save',
        payload: { systemParameters: { ...systemParameters, [props]: value } },
      });
      return value;
    }
    return value;
  } catch (error) {
    console.error(error);
  }
}

/**
 *getSystemParamsToValue
 *获取系统参数值
 * @param {string}
 * name
 * @returns
 */
export async function getSystemParamsToValue(name: string) {
  const params = getModelData('config.systemParameters');
  let value = _.get(params, `${name}`);
  if (!value) {
    value = await getSystemParamsByValue(name);
  }
  return value;
}
